package com.example.springflowable.ask_for_leave.service.impl;

import com.example.springflowable.ask_for_leave.service.TaskListService;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname TaskServiceImpl
 * @Description TODO
 * @Date 2022/5/6 15:16
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class TaskListServiceImpl implements TaskListService {

    @Autowired
    private TaskService taskService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    HistoryService historyService;


    @Override
    public List<Task> findMyPersonalTask(String USERNAME) {
        return taskService.createTaskQuery()     // 创建查询对象
                .taskAssignee(USERNAME)         // 指定办理人
                .list();
    }

    @Override
    public void completeMyPersonalTask(String taskId) {
        taskService.complete(taskId);
    }

    @Override
    public void deleteProcessInstance(String processId, String reason) throws Exception {
        runtimeService.deleteProcessInstance(processId, reason);
    }

    /**
     * @param PROC_INST_ID_ 流程实例ID
     * @throws Exception
     */
    @Override
    public void deleteHiProcessInstance(String PROC_INST_ID_) throws Exception {
        historyService.deleteHistoricProcessInstance(PROC_INST_ID_);
    }


    @Override
    public List<HistoricActivityInstance> getHistory() {
      ProcessEngine processEngine = getProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                // 从 act_hi_actinst 找到 proc_def_id_ 字段
                .processDefinitionId("holidayRequest:1:7503")

                // 查询的历史记录状态是已经完成的
                .finished()

                // 指定排序的字段和顺序
                .orderByHistoricActivityInstanceEndTime().asc()
                .list();
        for (HistoricActivityInstance historicActivityInstance : list) {

            // 获取到活动名称 活动状态的id 这个节点所花费的时间
            System.out.println(historicActivityInstance.getActivityName()+"historicActivityInstance.getActivityId() = " + historicActivityInstance.getActivityId()+":"+historicActivityInstance.getDurationInMillis());
        }
        return list;
    }

    @Override
    public void processEngineDeploy() {

        // 1.获取ProcessEngine对象
        ProcessEngine processEngine = getProcessEngine();
        // 2.获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();

        // 3.完成流程的部署操作
        Deployment deploy = repositoryService.createDeployment()
                // 关联要部署的流程文件
                .addClasspathResource("holiday-request.bpmn20.xml")
                .name("请求流程")
                // 部署流程
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
    }


    private  ProcessEngine getProcessEngine(){
        // 获取 ProcessEngineConfiguration对象
        ProcessEngineConfiguration processEngineConfiguration = new StandaloneProcessEngineConfiguration();

        // 配置数据库
        processEngineConfiguration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        processEngineConfiguration.setJdbcUsername("root");
        processEngineConfiguration.setJdbcPassword("admin");
        processEngineConfiguration.setJdbcUrl("jdbc:mysql:///spring-flowable?serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true");
        // 如果数据库表结构方式不存在就新建
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 通过 ProcessEngineConfiguration构建我们需要的 processEngine对象
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        return processEngine;
    }
}
